package com.mymain.demo.controller;

import com.mymain.demo.entity.User;
import com.mymain.demo.service.UserService;
import com.mymain.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author : JiangLinHan
 * Date : 2019/4/10
 * Time : 9:43
 **/
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询所有数据
     *
     * @return
     */
    @RequestMapping(value = "findAll", produces = "application/json; charset=UTF-8")
    public Map<String, Object> findAll() {
        Map<String, Object> map = new HashMap<>();
        List<User> userList = userService.findAll();
        map.put("data", userList);
        map.put("size", userList.size());
        return map;
    }

    /*
     *查询id在num数值之后的所有user数据
     */
    @RequestMapping(value = "findAllByIdAfter", produces = "application/json;charset=UTF-8")
    public Result findAllByIdAfter(Integer num) {
        Result result = new Result();
        Map<String, Object> map = new HashMap<>();
        try {
            //c+a  +  t快捷try
            List<User> userList = userService.findAllByIdAfter(num);
            map.put("userList", userList);
            result.setData(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("sorry this is fail");
            return result;
        }
    }

    /**
     * 添加数据
     */
    @RequestMapping(value = "save", produces = "application/json;charset=UTF-8")
    public void save() {
        User user = new User();
        user.setId(99887);
        user.setAddress("湖南省长沙市");
        user.setUsername("1231232");
        userService.save(user);
    }

    /**
     * 通过id查询user
     */
    @RequestMapping(value = "findAllById", produces = "application/json;charset=UTF-8")
    public Map<String, Object> findAllById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        User user = userService.findAllById(id);
        map.put("data", user);
        return map;
    }


    /**
     * 通过id删除user
     */
    @RequestMapping(value = "deleteById", produces = "application/json;charset=UTF-8")
    public void deleteById(Integer id) {
        userService.deleteById(id);
    }

    /**
     * 修改
     */
    @RequestMapping(value = "update", produces = "application/json;charset=UTF-8")
    public void update() {
        User user = userService.findAllById(4);
        user.setAddress("湖南省长沙市");
        userService.update(user);
    }



    /**
     * 前后台连通之登录
     */
    @RequestMapping(value = "/user/login")
    public Object loginUser(User user) {
        Map<String,Object> result = new HashMap<String, Object>();
        //查询后台用户对象
        User usereq = userService.findUserByUsername(user.getUsername());
        System.out.println(usereq);
        if( usereq != null){
            //如果user不为空，则匹配账号密码
            if(usereq.getUsername().equals(user.getUsername()) && usereq.getPassword().equals(user.getPassword()) ) {
                result.put("code", 200);
                result.put("msg", "登录成功");
                result.put("token", "adminxxxx");
                return result;
            }
        }
        result.put("code", 500);
        result.put("msg", "登录失败");
        return result;
    }



}
